import {sendCode,Get_register,Login,getUserInfo,logout} from '@/API/index'
export default {
    namespaced:true,
    state:{
        Code:'',
        token:sessionStorage.getItem('TOKEN'),//把Session储存数据的TOKEN值赋值给token
        info:{}
    },
    actions:{
        // 获取验证码数据
       async getcode({commit},phone){
           let code =  await sendCode(phone);
              if (code.data.code==200) {
                commit('GETCODE',code.data.data)
                return 'OK'
           }else{
            return Promise.reject(new Error('faile'))
           }
        },
        //注册
       async getregister({},data){
            return await Get_register(data);
        },
        //登录
       async goligin({commit},data){
           return await Login(data);
        },
        //获取token的数据
       async getuserinfo({commit}){
            let info =  await  getUserInfo()
            if (info.data.code==200) {
                commit('GETUSERINFO',info.data.data)
            }else{
                sessionStorage.removeItem('TOKEN');
            }
        },
        //退出登录
       async loginout({state}){
         let out =   await logout()
         if (out.data.code==200) {
            state.info = {}     //info的数据赋值为空   
            state.token=''      //token的数据赋值为空
            sessionStorage.removeItem('TOKEN')    //移出Session存储的TOKEN数据
             return 'ok'
         }else{
             return Promise.reject(new Error('faile'))
         }
        }
    },
    mutations:{
        GETCODE(store,getcode){
            store.Code = getcode
        },
        GETUSERINFO(store,getinfo){
            store.info = getinfo
        }
    },
    getters:{
        
    }
}